const num = ['0','1','2','3','4','5','6','7','8','9'];
const en = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
const EN = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
const special = ['@','_','#','!','&'];

const getOne = arr => {
    return arr[Math.floor(Math.random() * arr.length)];
}

const getPwd = (min=8, max=8) => {
    let pool = num.concat(en).concat(EN).concat(special);

    let arr = [];
    arr.push(getOne(num));
    arr.push(getOne(en));
    arr.push(getOne(EN));
    arr.push(getOne(special));

    let len = min + Math.floor(Math.random() * (max - min + 1));

    for(let i = 4; i < len; i++){
        arr.push(pool[Math.floor(Math.random() * pool.length)]);
    }

    let result = [];
    for(let j=0; j<len; j++){
        result.push(arr.splice(Math.random() * arr.length, 1)[0]);
    }

    return result.join('');
}

module.exports = getPwd;